/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package e.z.blog.back.action.base;

import e.z.blog.back.action.AbstractAction;
import e.z.blog.back.bean.system.Manager;
import e.z.blog.back.com.ComGetIpApi;
import e.z.blog.back.com.ComVerfiyCodeApi;
import e.z.blog.back.com.DaoManagerApi;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import z.h.w.jar.data.time.Time;
import z.h.w.jar.kit.Self;

/**
 * 这个页面不进行权限管理，普通用户都能到达这个页面，除了这个页面以外所有的页面都要进行
 *
 * 权限管理。2012.02.21
 *
 * @author EnzoZhong
 */
@IocBean
@At( "" )
public class BackLogin extends AbstractAction {

        @Inject
        private ComVerfiyCodeApi comVerfiyCode;
        @Inject
        private ComGetIpApi comGetIp;
        @Inject
        private DaoManagerApi daoManager;

        @At( "" )
        @Ok( "jsp:back.login" )
        public Map<String , Object> page ( HttpSession session , HttpServletRequest req ) {
//                Mvcs.setLocaleName( session , "zh_CN" );
                Map<String , Object> re = this.createMap( req , "" );
                re.put( "time" , Time.born() );
                re.put( "ip" , comGetIp.getIP( req ) );
                re.put( "age" , Time.howOldAmI() );
                return re;
        }

        /**
         *
         * @return
         */
        @Ok( "json" )
        @At( "/fonts" )
        @AdaptBy( type = JsonAdaptor.class )
        public String[] getFonts () {
                return Self.getAllFontName();
        }

        /**
         * 暂时还没有写权限系统，基本登陆，等CRUD和基本功能完善后，写权限系统，并把该
         *
         * 方法改写。2012.02.21
         */
        @Ok( "json" )
        @At( "/login" )
        @AdaptBy( type = JsonAdaptor.class )
        public Map<String , Object> login (
                @Param( "manager" ) Manager admin , @Param( "code" ) String code , HttpSession session ) {

                Boolean booleanAdmin = daoManager.verfiy( admin );
                Boolean booleanCode = comVerfiyCode.verfiy( session , code );

                Map<String , Object> re = new HashMap();
                String successUrl = "/back/index";
                String failUrl = "/back";
                String msg = "";
                Boolean res = booleanAdmin & booleanCode;

                if ( !booleanAdmin ) {
                        msg = msg + "账户或密码错误！\r\n";
                }
                if ( !booleanCode ) {
                        msg = msg + "验证码错误！";
                }

                re.put( "res" , res );

                if ( res ) {
                        re.put( "url" , successUrl );
                } else {
                        re.put( "tip" , msg );
                        re.put( "url" , failUrl );
                }
                return re;
        }

        /**
         *
         * @param request < p/>
         * <p/>
         * @return
         */
        @Ok( "json" )
        @At( "/ip" )
        @AdaptBy( type = JsonAdaptor.class )
        public String getIp ( HttpServletRequest request ) {
                return comGetIp.getIP( request );
        }

        /**
         * 输出PNG验证码图片
         * <p/>
         * @param rep param session
         * @param sess
         */
        @Ok( "void" )
        @At( "/code" )
        public void outputCode ( HttpServletResponse rep , HttpSession sess ) {
                try {
                        OutputStream os = rep.getOutputStream();
                        ImageIO.write( comVerfiyCode.create( sess ) , "PNG" , os );
                } catch ( IOException ex ) {
                }
        }

        @Override
        public Map<String , Object> page ( HttpServletRequest req ) {
                throw new UnsupportedOperationException( "Not supported yet." );
        }
}
